import promise from "es6-promise"
import "whatwg-fetch"

let urlHead = "https://fo.huawanghaochen.com:8843"

export const getResponse = async (url, token) => {
	const response = await fetch(urlHead+url, {
		method: "POST",
		headers: {
			"Content-Type": "application/x-www-form-urlencoded",
			'token': token || ''
		}
	}).catch((error) => {
		console.log(error)
	})

	try{
		var jsonp = await response.json()
		if(jsonp.code === 1003 || jsonp.code === 1004) {
			window.location="/"
			return;
		} 

		return jsonp
	}catch(error) {
		console.log(error)
	}

}

export const getResponses = async (url, params, token) => {
	const response = await fetch(urlHead+url, {
		method: "POST",
		headers: {
			"Content-Type": "application/x-www-form-urlencoded",
			'token': token || ''
		},
		body: params
	}).catch((error) => {
		console.log(error)
	})
	
	try{
		var jsonp = await response.json()
		if(jsonp.code === 1003 || jsonp.code === 1004) {
			window.location="/"
			return;
		} 
		return jsonp
	}catch(error) {
		console.log(error)
	}
}

export const getResponsesFile = async (url, params) => {
	const response = await fetch(urlHead+url, {
		method: "POST",
		body: params
	}).catch((error) => {
		console.log(error)
	})

	return await response.json().catch((error) => {
		console.log(error)
	})
}
